<?php

class MY_Controller Extends CI_Controller
{
	
	var $school_name = FALSE;
	var $school_address = FALSE;
	var $school_telephone = FALSE;
	var $school_email = FALSE;
	var $current_grading_period = FALSE; // returns id of current grading period
	var $current_school_year = FALSE; // returns id of current school year
	var $schoolyear_date = FALSE; // returns school year format of 0000 - 0000
	var $gperiods_current; // Returns the gp_desc of current grading period
	var $userid;
	var $offline_status = FALSE;
	
	protected $layout_view = 'application';
    protected $content_view ='';
    protected $view_data = array();
	
	public function __construct(){
		parent::__construct();
		$this->_check_online_status();
		
		// $this->output->enable_profiler('TRUE');
		$this->load->model(array('M_school_settings','M_gperiods','M_school_years'));
		
		$this->current_grading_period 	= $this->M_gperiods->get_id_of_current_gperiod();
		$this->current_school_year		= $this->M_school_years->get_id_of_current_sy();
		$this->schoolyear_date			= $this->M_school_years->get_current_sy_format();
		$this->gperiods_current	= $this->M_gperiods->get_current_gperiods_value('gp_desc');
		$this->userid = $this->session->userdata('userid');
		$id = 1;
		$query = $this->M_school_settings->get_all_school_setting($id);
		
		if(!empty($query))
		{
			foreach ($query as $row) 
			{
				$this->school_name = $row->school_name;
				$this->school_address = $row->school_address;
				$this->school_telephone = $row->school_telephone;
				$this->school_email = $row->school_email;
			}
		}else{
			$this->school_name = NULL;
			$this->school_address = NULL;
			$this->school_telephone = NULL;
			$this->school_email = NULL;
		}
	}
	
	public function _output($output)
    {
		$this->view_data['__page_title'] = $this->_accronymizer($this->school_name).' | Student Portal';
        if($this->content_view !== FALSE && empty($this->content_view)) $this->content_view = $this->router->class . '/' . $this->router->method;
        
        $yield = file_exists(APPPATH . 'views/' . $this->content_view . EXT) ? $this->load->view($this->content_view, $this->view_data, TRUE) : FALSE ;
        
        if($this->layout_view)
            echo $this->load->view('layouts/' . $this->layout_view, array('yield' => $yield), TRUE);
        else
            echo $yield;
			echo $output;
    }
	
	protected function _check_online_status()
	{	
		$this->load->model('M_school_settings');
		$check = $this->M_school_settings->check_online_status();
		
		if($check->status == FALSE)
		{
	
			$this->layout_view = 'application_no_side_bar';
			$this->offline_status = TRUE;
			$this->view_data['message'] =  $this->M_school_settings->get_custom_message('offline_portal');
			if(strtolower($this->router->class) == 'offline')
			{
				
			}else{
				redirect('offline');
			}
		}else{
			$this->offline_status = FALSE;
			return NULL;
		}
	}
	
	public function _msg($type = FALSE,$message = FALSE,$redirect = FALSE)
	{
		$type = strtolower($type);
		switch($type)
		{
		 	case $type == 'e':
				$template = "<div data-alert class='alert-box alert'><i class='icon-exclamation-sign'></i> ".(($message))." <a href='#' class='close'>&times;</a></div>";
			break;
			case $type == 's':
				$template = "<div data-alert class='alert-box success'><i class='icon-ok'></i> ".(($message))."</div>";
			break;
			case $type == 'n':
				$template = "<div class='alert-box'><i class='icon-info-sign'></i> ".(($message))."</div>";
			break;
			case $type == 'p':
				$template = $message;
			break;
			case $type == 'x';
				$template = (($message));
			break;
		}
		
		if($type AND $message AND $redirect)
		{
			$this->session->set_flashdata('system_message',$template);
			redirect($redirect);
		}elseif($type AND $message AND $redirect == FALSE){
			return $template;
		}
		
		if($redirect == FALSE AND $message == FALSE AND $redirect == FALSE)
		{
			return $this->session->flashdata('system_message');
		}
	}
	
	private function _accronymizer($string = FALSE)
	{
		if($string !== FALSE)
		{
			$clean_string = htmlspecialchars(strip_tags($string));
			$number_of_words = str_word_count($clean_string,1);
			function acc($x)
			{
				if(strlen($x) > 1)
				{
					return $x[0];
				}
			}
			$accronym = array_map("acc",$number_of_words);
			return strtoupper(implode('',$accronym));
		}else{
			return NULL;
		}
	}
	
	
}


class MY_Parent Extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
	}
	
}